﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Diagnostics;
using System.ComponentModel;

namespace ArmoryViewer
{
    /// <summary>
    /// Interaction logic for ProgressWindow.xaml
    /// </summary>
    public partial class ProgressWindow : Window
    {
        TaskDialog dlg;
        int curVal, maxVal;
        string caption, text;
        TaskDialogProgressBarState pgsState;
        TaskDialogStandardButtons buttons;

        public EventHandler<TaskDialogClosingEventArgs> TaskDialogClosing { get; set; }
        public CancelEventHandler FormClosing { get; set; }
        public EventHandler TaskDialogOpened { get; set; }

        /// <summary>
        /// Gets or sets the current progress value.
        /// </summary>
        /// <value>The current value.</value>
        public int CurrentValue
        {
            get { return curVal; }
            set 
            { 
                curVal = value;
                pgsProgress.Value = value;
                if (dlg != null && dlg.ProgressBar != null)  dlg.ProgressBar.Value = value;
            }
        }

        /// <summary>
        /// Gets or sets the maximum progress value.
        /// </summary>
        /// <value>The maximum progress value.</value>
        public int MaxValue
        {
            get { return maxVal; }
            set 
            { 
                maxVal = value;
                pgsProgress.Maximum = value;
                if (dlg != null) dlg.ProgressBar.Maximum = value;
            }
        }

        /// <summary>
        /// Gets or sets the caption.
        /// </summary>
        /// <value>The caption.</value>
        public string Caption
        {
            get { return caption; }
            set 
            { 
                caption = value;
                lblTitle.Content = value;
                if (dlg != null) dlg.InstructionText = value;
            }
        }

        /// <summary>
        /// Gets or sets the dialog text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get { return text; }
            set 
            { 
                text = value;
                lblText.Content = value;
                if (dlg != null) dlg.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the standard buttons.
        /// </summary>
        /// <value>The standard buttons.</value>
        public TaskDialogStandardButtons StandardButtons
        {
            get { return buttons; }
            set
            {
                buttons = value;
                if (value == TaskDialogStandardButtons.Cancel)
                    btnCancel.Content = "Cancel";
                else
                    btnCancel.Content = "Close";
                
            }
        }

        /// <summary>
        /// Gets or sets the state of the progress.
        /// </summary>
        /// <value>The state of the progress.</value>
        public TaskDialogProgressBarState ProgressState 
        {
            get { return pgsState; }
            set 
            {
                TaskDialogStandardIcon ico = TaskDialogStandardIcon.Information;
                pgsState = value;
                if (ProgressState == TaskDialogProgressBarState.Marquee)
                    pgsProgress.IsIndeterminate = true;
                else if (ProgressState == TaskDialogProgressBarState.Error)
                {
                    pgsProgress.Value = 0;
                    ico = TaskDialogStandardIcon.Error;
                }

                
                if (dlg != null && dlg.ProgressBar != null)
                {
                    dlg.ProgressBar.State = pgsState;
                    dlg.Icon = ico;
                }
            }
        }
       

        /// <summary>
        /// Initializes a new instance of the <see cref="ProgressWindow"/> class.
        /// </summary>
        public ProgressWindow()
        {
            InitializeComponent();
            this.Closing += FormClosing;
            ProgressState = TaskDialogProgressBarState.Normal;
            Caption = "Loading";
            Text = "Doing something...";
            MaxValue = 100;
            CurrentValue = 0;
        }

        /// <summary>
        /// Shows the progress dialog.
        /// </summary>
        public void ShowMe()
        {
            dlg = null;
            if (App.IsWindowsVista)
            {
                try
                {
                    ShowTaskDialog();
                }
                catch (NotSupportedException)
                {
                    ShowDialog();
                }
            }
            else
                ShowDialog();
        }

        private void ShowTaskDialog()
        {
            dlg = new TaskDialog();
            dlg.Caption = "Armory Viewer";
            dlg.Icon = TaskDialogStandardIcon.Information;
            dlg.ProgressBar = new TaskDialogProgressBar(0, maxVal, curVal);
            dlg.OwnerWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
            dlg.StartupLocation = TaskDialogStartupLocation.CenterOwner;
            dlg.InstructionText = caption;
            dlg.Text = text;
            dlg.StandardButtons = TaskDialogStandardButtons.Cancel;
            dlg.Closing += TaskDialogClosing;
            dlg.ProgressBar.State = pgsState;
            dlg.Opened += TaskDialogOpened;
            dlg.Show();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lblTitle.Content = caption;
            lblText.Content = text;
            pgsProgress.Minimum = 0;
            pgsProgress.Maximum = maxVal;
            pgsProgress.Value = curVal;
            
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Closes the dialog.
        /// </summary>
        public void CloseDialog()
        {
            try { dlg.Close(); }
            catch { }
            this.Close();
        }

    }
}
